<?php

/**
 * 用户信息表
 */
use Common\Bridge;
use lib\core\AdminAction;
use Center\User;

define('SITE_AUTH', 'user-info');
require('../../../common.inc.php');

class userInfo extends AdminAction
{
    private $pdo_r;

    private $field = [
        'UserId' => '用户ID',
        'NickName' => '昵称',
        'Sex' => '性别',
        'Mobile' => '手机',
        'BirthDay' => '生日',
        'Height' => '身高',
        'Weight' => '体重',
        'Status' => ['Nick' => '状态', 'Data' => [0 => '等待完善信息', 1 => '等待绑定体脂秤', 2 => '已完成体脂秤绑定']],
        'SignData' => '打卡记录',
        'LoginTime' => '最近登录时间'
    ];

    private $sex = [
      0=>'未知',
      1=>'男',
      2=>'女',
    ];

    private $primary_key = 'UserId';


    public function doDefault()
    {
        $param['UserId'] = (int)$_GET['UserId'];
        $param['NickName'] = $_GET['NickName'];
        $param['Mobile'] = $_GET['Mobile'];
        $param['Sex'] = $_GET['Sex'];


        $field = $this->field;
        $field_key = array_keys($field);
        $field_key_str = '`' . implode('`,`', $field_key) . '`';

        $page = empty($_GET['page']) ? 1 : (int)$_GET['page'];
        $limit = 20;
        $offset = ($page - 1) * $limit;

        $where = "1";
        if ($param['UserId'] > 0) {
            $where .= " AND UserId = " . $param['UserId'];
        } else {
            $param['UserId'] = '';
        }

        if ($param['NickName'] != '') {
            $where .= " AND UserNick LIKE '%" . $param['NickName'] . "%'";
        }

        if ($param['Mobile']) {
            $where .= " AND Mobile = '" . $param['Mobile'] . "'";
        }

        if ($param['Sex']) {
            $where .= " AND Sex = " . $param['Sex'];
        }

        $primary_key = $this->primary_key;
        $count_sql = "SELECT COUNT($primary_key) AS `num` FROM `User` WHERE $where";

        $count = $this->readDb($count_sql);
        $count = (int)$count[0]['num'];

        $sql = "SELECT * FROM `User` WHERE $where LIMIT $offset,$limit";
        $data = $this->readDb($sql);

        $smarty = $this->app->page();
        if ($count > 0) {
            if (!empty($data)) {
                $smarty->value('list', $data);
            }

            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            $smarty->value('page_info', $page_info);

            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('pagination', $pagination);
        }

        $smarty->value('sex',$this->sex);
        $smarty->value('param', $param);
        $smarty->value('field', $field);
        $smarty->output('user/userList.tpl');
    }

    /**
     * 成员列表
     */
    public function doMember()
    {
        $uid = (int)$_GET['UserId'];
        if ($uid < 1) {
            HDshowMsg('操作失败！', ['type' => 'error', 'desc' => 'UserId为空']);
        }

        $sql = "SELECT * FROM User WHERE UserId = $uid";
        $list = $this->readDb($sql);

        $smarty = $this->app->page();
        $smarty->value('list', $list);
        $smarty->output('user/userMemberList.tpl');
    }


    /**
     * 利用SQL语句读取数据库并返回相应数据
     *
     * @param $sql
     * @return mixed
     */
    private function readDb($sql)
    {
        $pdo = $this->pdo_r;
        if (empty($pdo)) {
            $pdo = $this->pdo_r = Bridge::pdo(Bridge::DB_USER_R);
        }

        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }
}

$app->run();